package javaweb.controller;

import javaweb.anno.Log;
import javaweb.pojo.Clazz;
import javaweb.pojo.ClazzPageResult;
import javaweb.pojo.ClazzQueryParam;
import javaweb.pojo.Result;
import javaweb.sevice.ClazzService;
import javaweb.sevice.StudentService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* 班级管理系统
* */
@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private StudentService studentService;

    /*
    * 班级管理分页查询
    * */
    @GetMapping
    public Result getClazzInfo(ClazzQueryParam clazzQueryParam) {
        log.info("分页查询获取到的参数：{}", clazzQueryParam);
        ClazzPageResult pageResult = clazzService.getClazzInfo(clazzQueryParam);
        return Result.success(pageResult);
    }

    /*
    * 添加班级
    * */
    @Log
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz) {
        log.info("新添加班级的信息为：{}", clazz);
        clazzService.addClazz(clazz);
        return Result.success();
    }

    /*
    * 根据id查询班级
    * */
    @GetMapping("/{id}")
    public Result getClazzById(@PathVariable Integer id){
        log.info("获取到的id为：{}", id);
        Clazz clazz = clazzService.getClazzById(id);
        return Result.success(clazz);
    }

    /*
    * 修改班级信息
    * */
    @Log
    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz) {
        log.info("修改内容为：{}", clazz);
        clazzService.updateClazz(clazz);
        return Result.success();
    }

    /*
    * 根据id删除班级信息
    * */
    @Log
    @DeleteMapping("/{id}")
    public Result deleteClazz(@PathVariable Integer id) {
        log.info("删除的班级id为：{}", id);
        clazzService.deleteClazzById(id);
        return Result.success();
    }

    /*
    * 查询所有班级信息
    * */
    @GetMapping("/list")
    public Result getClazzList() {
        log.info("查询所有班级信息");
        List<Clazz> list = studentService.getClazzList();
        return Result.success(list);
    }
}
